package com.mq.consumer;

/**
 * ClassName: ConsumerListener
 * Package: com.mq.consumer
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2024/8/6 - 23:58
 * @Version: v1.0
 */
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 消费者监听器
 */
@Slf4j
@Component
public class ConsumerListener {

    /**
     * 队列名称
     */
    public static final String QUEUE_NAME = "basic-consumer-queue";
    public static final String EXPIRE_EXCHANGE_NAME = "consumer-expired-exchange";
    public static final String EXPIRE_QUEUE_NAME = "consumer-expired-queue";
    public static final String EXPIRE_ROUTING_NAME = "consumer-expired-routing";



    /**
     * 消费者监听方法
     *      1、直接使用queues指定队列，需要通过rabbitMQ
     *         页面首先添加这个队列名称以及绑定关系，否则报错
     * @param msg 接收到的消息体
     * @param message 消息对象
     * @param channel 通道对象
     */
//    @RabbitListener(queues = QUEUE_NAME)
//    public void handlerMessage(String msg, Message message, Channel channel) throws IOException {
//        //获取当前消息的deliveryTag，根据deliveryTag追溯每一条消息
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//
//        try {
//            //假设以下是业务核心操作
//            log.info("核心业务操作接收到消息：{}", msg);
//
//            //模式演示核心业务操作失败，消息重新回到队列
//            System.out.println(10 / 0);
//
//            /*
//             *  核心业务操作成功，返回确认消息。参数2表示是否批量确认，true表示批量确认
//             */
//            channel.basicAck(deliveryTag, false);
//
//        }catch (Exception e) {
//            log.error("核心业务操作失败，消息重新回到队列，消息体：{}", msg);
//            //获取当前消息是否是重新投递的
//            Boolean redelivered = message.getMessageProperties().getRedelivered();
//            if(redelivered) {
//                log.error("消息不再投递，消息体：{}", msg);
//                //参数3：消息是否需要重写投递
//                channel.basicNack(deliveryTag, false, false);
//            }else{
//                log.info("消息第一次投递，消息体：{}", msg);
//                channel.basicNack(deliveryTag, false, true);
//            }
//
//            throw new RuntimeException(e);
//        }
//    }

//    @RabbitListener(queues = QUEUE_NAME)
//    public void handlerMessage(String msg, Message message, Channel channel) throws IOException, InterruptedException {
//        log.info("削峰限流消息：{}", msg);
//        TimeUnit.SECONDS.sleep(1);
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = EXPIRE_QUEUE_NAME,
                    durable = "true"),
            exchange = @Exchange(name = EXPIRE_EXCHANGE_NAME),
            key = EXPIRE_ROUTING_NAME
    )
    )
    public void handlerMessage(String msg, Message message, Channel channel) throws IOException, InterruptedException {
        log.info("超时设置：{}", msg);
    }
}
